var vrData = require('../../data/orderListData.js');
var app = getApp();

Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 1,
    tabTitle: ['待付款', '已付款'],
    orderType: ["0", "9", "9", "10", "11"],
    orderList: [],
    oneList: null,
    startnum: '0',
    emptyShow: true,
    loadingShow:false
  },

  onLoad: function (option) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
        });
      }
    });
  },

   onShow: function(option) {
    var that = this;
    that.setData({
      startnum: '0',
      orderList: [],
      currentTab: 1,
      loadingShow: false
    })

    that.getOrderList();
  },

  onHide: function() {
    var that = this;
      that.setData({
      currentTab: 1
    })
  },

  // 左右滑动
  bindChange: function (e) {
    var that = this;
    that.setData({
      startnum: '0',
      orderList: [],
      currentTab: e.detail.current,
    });


    // wx.showLoading({
    //   title: '加载中',
    // })
    // setTimeout(function () {
    //   wx.hideLoading()
    // }, 3000)

    that.setData({
      loadingShow: false
    })

    that.getOrderList();
  },

  // 切换顶部tab
  swichNav: function (e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return;
    }
    that.setData({
      orderList: [],
      currentTab: e.target.dataset.current,
      startnum: '0'
    })

    // wx.showLoading({
    //   title: '加载中',
    // })
    // setTimeout(function () {
    //   wx.hideLoading()
    // }, 3000)
            that.setData({
          loadingShow: false
        })
    that.getOrderList();
  },

  // 进入订单详情页
  showOrderDetail: function (event) {
    if (!this.data.currentTab) {
      return;
    }
    var orderInfo = event.currentTarget.dataset.orderinfo;
    wx.navigateTo({
      url: "/pages/Order/OrderDetail/detail?orderInfo=" + JSON.stringify(orderInfo)
    })
  },

  // 获取订单数据
  getOrderList: function (type) {
    var that = this;

    // 模拟数据
    // var list = vrData.orderList;
    // console.log(vrData);
    // that.setData({
    //   orderList: list
    // })

    var selectTabIndex = that.data.currentTab;
    var orderType = that.data.orderType;
    var type = orderType[selectTabIndex];
    var url = app.ApiUrl.getUserOrderList;
    var startnum = that.data.startnum;

    var data = { "orderstate": type, "startnum": startnum, "requestnum": "10" };
    app.WxService.sendRrquest(url, 'GET', data, false)
      .then(function (response) {
        wx.hideLoading()
        that.setData({
          loadingShow: true
        })
        if (response.data.data.TotalCount == 0) {
          that.setData({
            orderList: [],
            startnum: '0',
            emptyShow: false
          })

          return;
        }

        var list = that.data.orderList;
        var newList = response.data.data.Datalist;
        var start = response.data.data.StartIndex;
        var startnum = that.data.startnum;
        if (start == startnum) {
          return;
        }

        var orderList = list.concat(newList);
        that.setData({
          emptyShow: true,
          orderList: orderList,
          startnum: response.data.data.StartIndex
        })
      });
  },

  loadMoreData: function (){
    this.getOrderList();
  },

  // 支付
  gotoPay: function (event) {
    var shopOrderCode = event.currentTarget.dataset.orderinfo;
    var that = this;
    var url = app.ApiUrl.getPayInfoByOrderCode;
    app.WxService.sendRrquest(url, 'POST', { shopOrderCode: shopOrderCode }, true)
      .then(function (response) {
        if (response.data.success) {
          var res = response.data.data;
          wx.requestPayment({
            'timeStamp': res.timeStamp,
            'nonceStr': res.nonceStr,
            'package': res.package,
            'signType': 'MD5',
            'paySign': res.paySign,
            'success': function () {
              wx.showToast({
                title: '支付成功',
                success: function () {

                }
              });
            },
            'fail': function (res) {
              wx.showToast({
                title: '支付失败',
                image: '/assets/images/Icon/error.png',
                title: '支付已取消',
              })
            }
          });
        } else {
          wx.showToast({
            title: '获取支付信息失败！',
          })
        }
      }, function () {

      })
  }
})